/*
 * // Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
 * // ---------------------------------------------------------------------------
 * // This program is free software; you can redistribute it and/or modify it
 * // under the terms of the GNU Lesser General Public License as published by the
 * // Free Software Foundation; either version 3 of the License, or (at your
 * // option) any later version.
 * // This library is distributed in the hope that it will be useful,
 * // but without any warranty; without even the implied warranty of merchantability
 * // or fitness for a particular purpose.
 * // You should have received a copy of the GNU Lesser General Public License along
 * // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
 * // ---------------------------------------------------------------------------
 */

package pl.bristleback.server.bristle.binding.resolvers;

import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.utils.StringUtil;

/**
 * This property resolver checks the type of the given value, returns the same object if value type is Double or uses {@link Double#parseDouble(String)} otherwise.
 * Every comma is replaced by dot.
 * <p/>
 * Created on: 2011-02-24 15:31:16 <br/>
 *
 * @author Wojciech Niemiec
 */
public class DoublePropertyResolver implements PropertyValueResolver<Double> {
  private static Logger log = Logger.getLogger(DoublePropertyResolver.class.getName());


  public Double extractValue(Object value) {
    if (value instanceof Double) {
      return (Double) value;
    } else if (value instanceof String) {
      String processedValue = ((String) value).replace(StringUtil.COMMA, StringUtil.DOT);
      try {
        return Double.parseDouble(processedValue);
      } catch (NumberFormatException e) {
        return null;
      }
    } else {
      return null;
    }
  }
}
